from typing import Tuple
import pygame
from view import View
from controller import Controller
from model import Model
from const import (
    WHITE,
    SCREEN_WIDTH, 
    SCREEN_HEIGHT,

    RESTART_GAME_EVENT,
)




class TicTacToeGame:

    screen: pygame.Surface # 游戏窗口
    # grid_manager: GridManager # 网格管理器
    # player_manager: PlayerManager # 玩家管理器
    model: Model # 数据模型
    renderer: View # 渲染器
    controller: Controller


    """
    游戏主控制器
    """
    def __init__(self):
        self.init()
        screen = self.screen
        self.renderer = View(screen)
        # self.grid_manager = GridManager()
        # self.player_manager = PlayerManager()
        self.model = Model()
        self.controller = Controller(self.model, self.renderer)  # 替换原来的多个handler
        

    
    def handle_events(self) -> bool:
        """
        处理Pygame事件
        """
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False
            
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:  # 检测R键按下
                    self.reset_game()
            
            if event.type == pygame.MOUSEMOTION:
                self.handle_mouse_motion(event.pos)
            
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.handle_mouse_click(event.pos)

            


            if event.type > pygame.USEREVENT:
                if event.type == RESTART_GAME_EVENT:
                    self.reset_game()

                # 处理其他事件
                self.controller.handle_event(event)
        
        return True

    def reset_game(self) -> None:
        """
        重置游戏状态
        """
        self.controller.init()
    
    def handle_mouse_motion(self, pos: Tuple[int, int]) -> None:
        """
        新的鼠标移动处理
        """
        self.renderer.handle_mouse_hover(pos, self.model)
    
    def handle_mouse_click(self, pos: Tuple[int, int]) -> None:
        """
        新的鼠标点击处理
        """
        self.renderer.handle_click(pos, self.model)
    


    def init(self) -> None:
        """
        初始化游戏
        """

        # 初始化pygame
        pygame.init()

        # 创建游戏窗口
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("TicTacToe")

        
    
    def run(self) -> None:
        """
        游戏主循环
        """
        clock = pygame.time.Clock()
        running = True
        
        while running:
            # 处理事件
            running = self.handle_events()
            
            # 清空屏幕
            self.screen.fill(WHITE)
            
            # 绘制游戏元素
            self.renderer.draw_view(self.model)
            self.renderer.draw_event_message(self.model.event_message)
            
            # 如果游戏结束，显示胜利信息
            if self.model.game_over:
                self.renderer.draw_game_over(self.model.winner)
            
            # 更新屏幕
            pygame.display.flip()
            
            # 控制帧率
            clock.tick(60)

        # 退出pygame
        pygame.quit()
        